﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using AppEnvironment.Lib;

namespace AppEnvironment.Lib
{
    public class NavParam
    {
        public virtual string GetName()
        {
            return string.Empty;
        }

        public virtual void ListToParams(NameValueCollection list)
        {
            //empty
        }

        public virtual void ParamsToList(NameValueCollection list)
        {
            //empty
        }

        public override sealed int GetHashCode()
        {
            return Navigation.GetUrl(this).GetHashCode();
        }


        public override sealed bool Equals(object obj)
        {
            NavParam other = obj as NavParam;
            if (other == null)
            {
                return false;
            }

            NameValueCollection theseParams = new NameValueCollection();
            ParamsToList(theseParams);

            NameValueCollection otherParams = new NameValueCollection();
            other.ParamsToList(otherParams);

            string[] existingKeys = theseParams.AllKeys;

            if (existingKeys.Length != otherParams.AllKeys.Length)
            {
                return false;
            }

            bool same = true;
            foreach (string key in existingKeys)
            {
                string existingValue = theseParams[key];
                string newValue = otherParams[key];
                same = (existingValue == newValue);
                if (!same)
                {
                    break;
                }
            }

            return same;
        }
    }
}
